import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution2273 {
    public List<String> removeAnagrams(String[] words) {
        List<String> ans=new ArrayList<>();
        ans.add(words[0]);
        for (String word : words) {
            if (!valid(word,ans.get(ans.size()-1))){
                ans.add(word);
            }
        }
        return ans;
    }

    private boolean valid(String a, String b) {
        if (a.length()!=b.length()){
            return false;
        }
        char[] ca=a.toCharArray();
        Arrays.sort(ca);
        char[] cb=b.toCharArray();
        Arrays.sort(cb);
        for (int i=0;i<ca.length;i++){
            if (ca[i]!=cb[i]){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2273().removeAnagrams(new String[]{"abba","baba","bbaa","cd","cd"}));
    }
}
